﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace VTFC
{
    // ----------------------------------------------------------------------------
    /// <summary>
    /// ListView sort
    /// </summary>
    // ----------------------------------------------------------------------------
    public class ListViewItemSorter : IComparer
    {
        /// <summary></summary>
        public int Index;
        /// <summary></summary>
        public SortOrder Order;

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="index">DisplayIndex property of <see cref="System.Windows.Forms.ListViewItem"/> instance.</param>
        /// <param name="order"><see cref="System.Windows.Forms.SortOrder"/> value.</param>
        // ----------------------------------------------------------------------------
        public ListViewItemSorter(int index, SortOrder order)
        {
            this.Index = index;
            this.Order = order;
        }//ctor

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Compare given values.
        /// </summary>
        /// <param name="x">Item x.</param>
        /// <param name="y">Item y.</param>
        /// <returns></returns>
        // ----------------------------------------------------------------------------
        public int Compare(object x, object y)
        {
            int value = String.Compare(((ListViewItem)x).SubItems[Index].Text, ((ListViewItem)y).SubItems[Index].Text);
            if (Order == SortOrder.Descending)
            {
                value *= 1;
            }
            return value;
        }//func Compare
    }//class ListViewItemSorter
}//namespace VTFC
